using System;
using System.Collections.Generic;
using System.Text;

using System.IO;
using System.Xml.XPath;
using System.Xml.Xsl;

namespace parser.html2xml2
{
    public class html2xml2Meths
    {
        public static string Htm2XmlString(string url)
        {
            WebCapt.agenceXML wx = new WebCapt.agenceXML();
            return wx.Capture(url);
        }
        public static string Htm2XmlStringQuery(string url, string query)
        {
            WebCapt.agenceXML wx = new WebCapt.agenceXML();
            wx.Capture(url);
            return wx.Query(query);
        }
        public static XPathNodeIterator Htm2XmlNodeset(string url)
        {
            WebCapt.agenceXML wx = new WebCapt.agenceXML();
            string strXml = wx.Capture(url);

            if (strXml == "(error)" || strXml.Length == 0 || strXml == "")
            {
                strXml = "<Error>Error Fetching url</Error>";
            }
            return NodesetFromString(strXml);
        }
        public static XPathNodeIterator Htm2XmlQueryNodeset(string url, string query)
        {
            WebCapt.agenceXML wx = new WebCapt.agenceXML();
            string xnode = "";
            if (query != null || query.Length > 0 || query != "")
            {
                string strXml = wx.Capture(url);
                if (strXml == "(error)" || strXml.Length == 0 || strXml == "")
                {
                    xnode = "<Error>Error Fetching url</Error>";
                }
                else
                {
                    string xPathSelectPs = query.Replace("&gt;", ">").Replace("&lt;", "<").Replace("&apos;", "\'").Replace("&quot;", "\"");
                    xnode = wx.Query(xPathSelectPs);
                    if (xnode == "(not found)" || strXml.Length == 0 || strXml == "")
                    {
                        xnode = "<Error>No data found for query</Error>";
                    }
                }
            }
            return NodesetFromString(xnode);
        }
        private static XPathNodeIterator NodesetFromString(string strXml)
        {
            StringReader stringReader = new StringReader(strXml);
            XPathDocument xPathDocument = new XPathDocument(stringReader);
            stringReader.Close();
            stringReader.Dispose();
            XPathNavigator xPathNavigator = xPathDocument.CreateNavigator();
            XPathExpression xPathExpression = xPathNavigator.Compile("/");
            XPathNodeIterator xPathNodeIterator = xPathNavigator.Select(xPathExpression);
            return xPathNodeIterator;
        }
        public static string resolveUri(string baseUrl, string relstr)
        {
            Uri uriout;
            Uri uribace = new Uri(baseUrl);
            if (Uri.TryCreate(uribace, relstr, out uriout))
            {
                return uriout.ToString();
            }
            else
            {
                return "";
            }
        }
        private static bool TryCreateCleanUri(Uri baseUrl, string relstr, out Uri result)
        {
            if (!Uri.TryCreate(baseUrl, relstr, out result))
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }
}
